package com.wyz.controller;

import com.sun.org.glassfish.gmbal.ParameterNames;
import com.wyz.dao.DepartmentDao;
import com.wyz.dao.EmployeeDao;
import com.wyz.pojo.Department;
import com.wyz.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;
    //查询所有员工并返回list页面
    @RequestMapping("/employees")
    public String listEmployees(Model model){
        Collection<Employee> employees = employeeDao.getAllEmployees();
        model.addAttribute("employees",employees);
        return "emp/list";
    }
    //跳转到添加页面
    @GetMapping("/emp")
    public String toAddPaper(Model model){
        //查询所有部门信息
        Collection<Department> departments =departmentDao.getDepartmentList();
        model.addAttribute("departments",departments);
        return "emp/add";
    }
    //添加员工操作
    @PostMapping("/emp")
    public String addEmployee(Employee employee){
        System.out.println(employee);
        employeeDao.save(employee);
        return "redirect:/employees";
    }
    //跳转到修改页面
    @GetMapping("/toUpdateEmp/{id}")
    public String toUpdateEmployee(@PathVariable("id") int id,Model model){
        Employee employee = employeeDao.getEmployeeById(id);
        model.addAttribute("emp",employee);
        Collection<Department> departments = departmentDao.getDepartmentList();
        model.addAttribute("departments",departments);
        return "emp/update";
    }
    //修改员工
    @RequestMapping("/updateEmp")
    public String updateEmploy(Employee employee){
        employeeDao.save(employee);
        return "redirect:/employees";
    }
    //删除员工
    @RequestMapping("/delEmp/{id}")
    public String delEmployee(@PathVariable("id") int id){
        employeeDao.delete(id);
        System.out.println("删除成功"+id);
        return "redirect:/employees";
    }
}
